<template>
  <div class="tab-control">
    <span v-for="(title, index) in titles" :key="title"
      :class="{active: selectTitle === title}" @click="itemClick(title)">{{title}}</span>
  </div>
</template>
<script>
export default {
  name: "TabControl",
  data: () => ({
    selectTitle: undefined
  }),
  props: {
    titles: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    itemClick(title) {
      if (this.selectTitle !== title) {
        this.selectTitle = title;
        // 提交事件
        this.$emit('tab-click', title);
      }
    }
  },
  created() {
    if (!this.selectTitle) this.selectTitle = this.titles[0];
  }
}
</script>
<style scoped>
  .tab-control {
    display: flex;
    text-align: center;
    font-size: 15px;
    height: 40px;
    line-height: 40px;
    background-color: white;
  }

  .tab-control span {
    flex: 1;
    padding-left: 5px;
    padding-right: 5px;
  }

  .active {
    color: var(--color-high-text);
    border-bottom: 2px solid var(--color-tint);
  }
</style>
